package com.fr.data.core;

import com.fr.base.ColumnRow;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.DateUtils;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.BaseXMLUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.report.adhoc.TransJSON;
import com.fr.report.cellElement.Formula;
import com.fr.report.core.AnDSColumn;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Compare
  implements XMLReadable, Serializable, Cloneable, TransJSON
{
  public static final int EQUALS = 0;
  public static final int NOT_EQUAL = 1;
  public static final int GREATER_THAN = 2;
  public static final int GREATER_THAN_OR_EQUAL = 3;
  public static final int LESS_THAN = 4;
  public static final int LESS_THAN_OR_EQUAL = 5;
  public static final int BEGINS_WITH = 6;
  public static final int NOT_BEGIN_WITH = 7;
  public static final int ENDS_WITH = 8;
  public static final int NOT_END_WITH = 9;
  public static final int CONTAINS = 10;
  public static final int NOT_CONTAIN = 11;
  public static final int IN = 12;
  public static final int NOT_IN = 13;
  public static final String XML_TAG = "Compare";
  private int op;
  private Object value;
  private Map nameMap = null;

  public Compare()
  {
  }

  public Compare(int paramInt, Object paramObject)
  {
    setOp(paramInt);
    setValue(paramObject);
  }

  public int getOp()
  {
    return this.op;
  }

  public void setOp(int paramInt)
  {
    this.op = paramInt;
  }

  public Object getValue()
  {
    return this.value;
  }

  public void setValue(Object paramObject)
  {
    if (paramObject == null)
      this.value = Primitive.NULL;
    else if (paramObject.toString().equalsIgnoreCase("nofilter"))
      this.value = Primitive.NOFILTER;
    else
      this.value = paramObject;
  }

  public boolean equals(Object paramObject)
  {
    if ((paramObject == null) || (!(paramObject instanceof Compare)))
      return false;
    Compare localCompare = (Compare)paramObject;
    return ((localCompare.getOp() == getOp()) && (ComparatorUtils.equals(localCompare.getValue(), getValue())));
  }

  public boolean eval(Object paramObject)
  {
    FArray localFArray;
    int i;
    int j;
    Object localObject = getValue();
    if (localObject instanceof Parameter)
      localObject = ((Parameter)localObject).getValue();
    if (paramObject instanceof Formula)
      paramObject = ((Formula)paramObject).getResult();
    if (paramObject == null)
      paramObject = Primitive.NULL;
    if (localObject == null)
      localObject = Primitive.NULL;
    if ((localObject == Primitive.NOFILTER) || (paramObject == Primitive.NOFILTER))
      return true;
    switch (getOp())
    {
    case 0:
      if (localObject instanceof FArray)
      {
        localFArray = (FArray)localObject;
        if (localFArray.length() > 0)
        {
          if (!(localFArray.toList().contains(paramObject)))
            break label176;
          return true;
        }
        localObject = null;
      }
      return ComparatorUtils.equals(paramObject, localObject);
    case 1:
      if (localObject instanceof FArray)
        if (((FArray)localObject).length() > 0)
          localObject = ((FArray)localObject).elementAt(0);
        else
          localObject = null;
      return (!(ComparatorUtils.equals(paramObject, localObject)));
    case 2:
      return (ComparatorUtils.compare(paramObject, localObject) > 0);
    case 3:
      return (ComparatorUtils.compare(paramObject, localObject) >= 0);
    case 4:
      return (ComparatorUtils.compare(paramObject, localObject) < 0);
    case 5:
      return (ComparatorUtils.compare(paramObject, localObject) <= 0);
    case 6:
      if ((paramObject == null) || (localObject == null))
        return false;
      return paramObject.toString().startsWith(localObject.toString());
    case 7:
      if ((paramObject == null) || (localObject == null))
        return false;
      return (!(paramObject.toString().startsWith(localObject.toString())));
    case 8:
      if ((paramObject == null) || (localObject == null))
        return false;
      return paramObject.toString().endsWith(localObject.toString());
    case 9:
      if ((paramObject == null) || (localObject == null))
        return false;
      return (!(paramObject.toString().endsWith(localObject.toString())));
    case 10:
      if ((paramObject == null) || (localObject == null))
        return false;
      return (paramObject.toString().indexOf(localObject.toString()) != -1);
    case 11:
      if ((paramObject == null) || (localObject == null))
        return false;
      return (paramObject.toString().indexOf(localObject.toString()) == -1);
    case 12:
      if (localObject instanceof FArray)
      {
        localFArray = (FArray)localObject;
        i = 0;
        j = 0;
        int k = localFArray.length();
        while (j < k)
        {
          if (ComparatorUtils.equals(paramObject, localFArray.elementAt(j)))
          {
            i = 1;
            break;
          }
          ++j;
        }
        return i;
      }
      if ((paramObject instanceof String) && (localObject instanceof String))
        return (((String)localObject).indexOf((String)paramObject) >= 0);
      return false;
    case 13:
      if (localObject instanceof FArray)
      {
        label176: localFArray = (FArray)localObject;
        i = 0;
        j = localFArray.length();
        while (i < j)
        {
          if (ComparatorUtils.equals(paramObject, localFArray.elementAt(i)))
            return false;
          ++i;
        }
        return true;
      }
      if ((paramObject instanceof String) && (localObject instanceof String))
        return (((String)localObject).indexOf((String)paramObject) < 0);
      return false;
    }
    return true;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    String str1;
    if (paramXMLableReader.isAttr())
      if ((str1 = paramXMLableReader.getAttr("op")) != null)
        setOp(Integer.parseInt(str1));
    if (paramXMLableReader.isChildNode())
    {
      String str2 = paramXMLableReader.getTagName();
      if (str2.equals("ColumnRow"))
      {
        int i = 0;
        int j = 0;
        if ((str1 = paramXMLableReader.getAttr("column")) != null)
          i = Integer.parseInt(str1);
        if ((str1 = paramXMLableReader.getAttr("row")) != null)
          j = Integer.parseInt(str1);
        setValue(ColumnRow.valueOf(i, j));
      }
      else if (str2.equals("AnDSColumn"))
      {
        AnDSColumn localAnDSColumn = new AnDSColumn();
        paramXMLableReader.readXMLObject(localAnDSColumn);
        setValue(localAnDSColumn);
      }
      else if ("Parameter".equals(str2))
      {
        setValue(ReportXMLUtils.readParameter(paramXMLableReader));
      }
      else if (("Object".equals(str2)) || ("O".equals(str2)))
      {
        setValue(ReportXMLUtils.readObject(paramXMLableReader));
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("Compare").attr("op", getOp());
    Object localObject = getValue();
    if (localObject instanceof ColumnRow)
    {
      ColumnRow localColumnRow = (ColumnRow)localObject;
      BaseXMLUtils.writeColumnRow(paramXMLPrintWriter, localColumnRow);
    }
    else if (localObject instanceof Parameter)
    {
      ReportXMLUtils.writeParameter(paramXMLPrintWriter, (Parameter)localObject);
    }
    else if (localObject instanceof AnDSColumn)
    {
      ((AnDSColumn)localObject).writeXML(paramXMLPrintWriter);
    }
    else
    {
      ReportXMLUtils.writeObject(paramXMLPrintWriter, getValue());
    }
    paramXMLPrintWriter.end();
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }

  public JSONObject createJSON()
    throws Exception
  {
    return null;
  }

  private int stringOP2Int(String paramString)
  {
    if (this.nameMap == null)
    {
      this.nameMap = new HashMap();
      this.nameMap.put("等于", new Integer(0));
      this.nameMap.put("不等于", new Integer(1));
      this.nameMap.put("大于", new Integer(2));
      this.nameMap.put("小于等于", new Integer(3));
      this.nameMap.put("小于", new Integer(4));
      this.nameMap.put("大于等于", new Integer(5));
    }
    return ((Integer)this.nameMap.get(paramString)).intValue();
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws Exception
  {
    if ((paramJSONObject.has("op")) && (paramJSONObject.get("op") instanceof String))
      setOp(stringOP2Int(paramJSONObject.getString("op")));
    if (paramJSONObject.has("value"))
      if (paramJSONObject.getString("type").equals("string"))
        setValue(paramJSONObject.getString("value"));
      else if (paramJSONObject.getString("type").equals("int"))
        setValue(new Integer(paramJSONObject.getInt("value")));
      else if (paramJSONObject.getString("type").equals("double"))
        setValue(new Double(paramJSONObject.getDouble("value")));
      else if (paramJSONObject.getString("type").equals("date"))
        setValue(DateUtils.DATEFORMAT2.parse(paramJSONObject.getString("value")));
      else if (paramJSONObject.getString("type").equals("boolean"))
        setValue(new Boolean(paramJSONObject.getBoolean("value")));
  }
}